#pragma once
#include "ast.hpp"
#include "../db_common.h"
#include <unordered_map>

class PlanBuilder {
public:
    PlanBuilder(const std::unordered_map<std::string, TableSchema> &schema);
    CompileResult build(std::unique_ptr<Stmt> &stmt);

private:
    const std::unordered_map<std::string, TableSchema> &db_schema;
    CompileResult buildSelect(const struct SelectStmt &sel);
    CompileResult buildCreate(const struct CreateTableStmt &ct);
    CompileResult buildInsert(const struct InsertStmt &ins);
    CompileResult buildCreateIndex(const struct CreateIndexStmt &stmt);
    CompileResult buildDrop(const Stmt &stmt);
    CompileResult buildAlter(const struct AlterTableStmt &stmt);
    CompileResult buildDelete(const struct DeleteStmt &del);
    CompileResult buildUpdate(const UpdateStmt &stmt);
    CompileResult buildShowTables(const struct ShowTablesStmt &);
    CompileResult buildShowIndices(const struct ShowIndicesStmt &stmt);

    CompileResult makeError(const std::string &type,const std::string &msg,int line,int col);
    Condition cloneCondition(const Condition *cond);
    std::string colTypeToString(ColType t,bool isUnsigned);
    ColumnSchema toSchema(const ColumnDef &c);
    ModifiedColumn toModifiedColumn(const ColumnDef &c, const std::string &newName="");
};
